home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Languguage OS 2
/
Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO
/
language
/
embedded
/
simulato
/
v2_3_mc6.tz
/
v2_3_mc6
/
testfiles
/
treefnd4.asm
< prev
next >
Wrap
Assembly Source File
|
1994-05-02
|
17KB
|
384 lines
***************************************************************************
*
* ASSIGNMENT: PROG3
* DUE DATE: NOVEMBER 16, 1992
* DESCRIPTION: This program stores data in a binary tree structure.
* The data to be stored are the integers 0-9. The
* subroutine MALLOC will be used to create the
* neccessary nodes at run time. Each node will
* consist of the left pointer address (4 bytes) the
* word-sized integer (2 bytes) and the right pointer
* address (4 bytes). The program will allow a user
* to enter commands from the keyboard as follows:
* 1. I x -- Will insert the integer x into
* the tree.
* 2. S x -- Will search the tree for x.
* 3. P -- Will print the tree in presearch
* format.
* 4. E -- Ends the program.
***************************************************************************
**** MAIN ROUTINE ****
* Functions as a place for processing commands and calling
* seperate modules to both insert search and print the tree.
**********************
**** RESOURCE ALLOCATION ****
* D2 -> holds user command and parameter after command process.
* A2 -> generic pointer to be used by any.
*****************************
ORG $1000
INIT_TREE CLR.L $5000 ;set up null node in tree to
MOVE.W #-1,$5004 ;trap operations before first
CLR.L $5006 ;insert.
START JSR WriteEOL ;make spaces so
JSR WriteEOL ;it's easier to read
LEA COMMANDMSG,A0 ;Load in prompt.
JSR WriteString ;Write string to screen.
LEA COMMAND,A0 ;load in place to store command
MOVE.L A0,-(A7) ;save this locator on stack.
JSR ReadString ;and then read in the string.
MOVEA.L (A7)+,A0 ;Restore pointer to command string.
CLR.L D2 ;Clear d2, to be used as command.
JSR READCOMMAND ;Parse command string.
MOVE.B (A0)+,D2 ;Load in read command character.
CASE_E CMPI.B #'E',D2 ;Compare command to exit code.
BNE.S CASE_I ;if not exit compare to nxt. case.
BRA.W END_IT ;else quit program.
CASE_I CMPI.B #'I',D2 ;Compare to insert command code.
BNE.S CASE_S ;if not look @ nxt. case.
MOVE.B (A0),D2 ;Load in parameter value from str.
JSR PUTNTREE ;Jump to place paramter into tree.
BRA.S START ;Return to nxt. commmand.
CASE_S CMPI.B #'S',D2 ;Compare to search for param. in
BNE.S CASE_P ;if not look @ nxt. case.
MOVE.B (A0),D2 ;load param. value from string.
JSR SEARCH ;search for parameter in string.
TST.W D1 ;test found integer flag.
BEQ.S S_NOFIND ;if zero. integer was not found
LEA INTEGERMSG,A0 ;place ptr. to found message.
JSR WriteString ;and write to the screen.
MOVE.B D2,D0 ;place low byte into d0 for print.
JSR WriteChar ;Print integer out.
LEA S_FOUNDMSG,A0 ;Load in tail of message.
JSR WriteString ;and print out.
BRA.W START ;return for nxt. command.
S_NOFIND LEA INTEGERMSG,A0 ;else place ptr. to not found msg.
JSR WriteString ;and write to screen.
MOVE.B D2,D0 ;place low byte into d0 for print.
JSR WriteChar ;print parameter
LEA S_NOFINDMSG,A0 ;Load in tail of message.
JSR WriteString ;and print message.
BRA.W START ;return for nxt. command.
CASE_P CMPI.B #'P',D2 ;Compare for parameter to print
BNE.W BAD_COMMAND ;if not get new command.
JSR PRINTREE ;Print tree.
BRA.W START ;return for nxt. command.
BAD_COMMAND LEA BAD_MSG,A0 ;Load bad command message.
JSR WriteString ;Write the message.
BRA.W START ;Get next command.
END_IT MOVE #228,D7 ;Quit program.
TRAP #14
NOLIST ;Include available subroutines.
DC.W 0
INCLUDE "samples.asm"
LIST
**** SUBROUTINE READCOMMAND ****
* This subroutine places the 20 character command string into the standard
* format of <ONE_LETTER_COMMAND><ONE_DIGIT_DECIMAL_NUMBER>. Where
* ONE_LETTER_COMMAND = {I,S,P,E},and ONE_DIGIT_NUMBER ={0,1,2,3,4,5,6,7,8,9}
* The parsed string will remain at the global location marked by the label
* COMMAND.
********************************
**** RESOURCE ALLOCATION ****
* A0 -> points to beginning of command string must be restored.
* A2 -> temporary position pointer.
*****************************
**** SUBROUTINES ****
* CALLS: ----
* CALL BY: MAIN
*********************
READCOMMAND MOVEA.L A0,A2 ;Load in position ptr.
MOVE.W #0,FOUND_DIGIT ;clear multiple digit flag.
NEXTPLACE MOVE.B (A0)+,D2 ;Load in next character.
TST.B D2 ;Test for end of string.
BEQ.W END_RCOMMAND ;if end go to end.
CMPI.B #'i',D2 ;Look for lower case insert command
BEQ.W SET_I ;if equal set up command string for in.
CMPI.B #'I',D2 ;or if equal to upper case do same.
BEQ.W SET_I ;by branching to same routine.
CMPI.B #'s',D2 ;Repeat case finding for s or S
BEQ.W SET_S ;by performin the same comparisons.
CMPI.B #'S',D2 ;The test needs to look @ a possible
BEQ.W SET_S ;8 test cases.
CMPI.B #'p',D2 ;Single operand commands will not be
BEQ.W SET_P ;distinguished.
CMPI.B #'P',D2 ;The routine will end regardless of
BEQ.W SET_P ;whether it finds a second operand
CMPI.B #'e',D2 ;or not when it hits the null charachter.
BEQ.W SET_E ;Set up to find specific letters because
CMPI.B #'E',D2 ;of the need to convert.
BEQ.W SET_E ;Numbers do not need to be converted
CMPI.B #'0',D2 ;because there is only one case.
BLT.S NEXTPLACE ;Therefore they will be tested in
CMPI.B #'9',D2 ;a range from 0-9.
BGT.S NEXTPLACE ;NO ERROR!!!!!!! WILL BE GIVEN FOR
SET_NUM MOVE.B D2,(A2) ;The first number found ends the command.
CMPI.W #1,FOUND_DIGIT ;Look to see if there is already a digit.
BEQ.S TOO_MANY_D ;if so end routine. else
MOVE.W #1,FOUND_DIGIT ;store a digit has already been entered
BRA.W NEXTPLACE ;and look for end of string.
TOO_MANY_D LEA MANY_DMSG,A0 ;Load ptr. for message.
JSR WriteString ;Print.
LEA COMMAND,A0 ;retore command ptr. to corrupt.
MOVE.B #'Z',(A0) ;corrupt command message.
END_RCOMMAND LEA COMMAND,A0 ;Restore the command string ptr.
CMPI.W #1,FOUND_DIGIT ;Make sure that a digit was input
BEQ.S GOOD_COMMAND ;if so end normal
LEA NO_DMSG,A0 ;else print out
JSR WriteString ;no digits found message.
LEA COMMAND,A0 ;restore the command string ptr.
MOVE.B #'Z',(A0) ;and corrupt command message.
GOOD_COMMAND ADDQ.L #1,A2 ;Point to next command.
MOVE.B #17,D2 ;Load the number of potential null char.
REPEAT_0 MOVE.B #0,(A2)+ ;zero byte increment pointer.
DBRA D2,REPEAT_0 ;repeat till end.
RTS
SET_I MOVE.B #'I',(A2)+
BRA.W NEXTPLACE
SET_E MOVE.B #'E',(A2)+
MOVE.W #1,FOUND_DIGIT
BRA.W NEXTPLACE
SET_S MOVE.B #'S',(A2)+
BRA.W NEXTPLACE
SET_P MOVE.B #'P',(A2)+
MOVE.W #1,FOUND_DIGIT
BRA.W NEXTPLACE
**** SUBROUTINE SEARCH ****
* This subroutine will search the tree starting from the root
* node @ address: $5000. The parameter it is to search must be of
* word length in d2. The subroutine will return in d1 a true flag
* if the param was found or a false flag if the param was not.
* The paramter will return where the said paramter should have been,
* left or right, in d3--a 0 corresponds to left, a 1 to right. The
* subroutine will return in a2 a pointer to the last node searched.
****************************
**** RESORCE ALLOCATION ****
* A2 -> pointer to next tested node.
* D2 -> the integer being searched for.
* D3 -> right/left flag
* D1 -> found flag.
****************************
**** SUBROUTINES ****
* CALLS: ----
* CALL BY: PUTNTREE
* MAIN
*********************
SEARCH MOVEA.L #$5000,A2 ;Load in top of tree.
CURRENT CMP.W 4(A2),D2 ;Look @ current node.
BGT.S GO_RIGHT ;If param is > node look to see if right.
BLT.S GO_LEFT ;If param is < node look to see if left.
ITS_EQ MOVE.W #1,D1 ;Set flag to true.
BRA.S END_SEARCH ;and quit search.
GO_RIGHT MOVE.W #1,D3 ;Move in looked @ right ptr.
TST.L 6(A2) ;Look @ right ptr. of current node.
BEQ.S SEARCH_NOFIND ;if zero. end routine w/o finding.
MOVEA.L 6(A2),A2 ;Load right ptr. into current ptr.
BRA.S CURRENT ;Look @ new node.
GO_LEFT CLR.W D3 ;Move in looked @ left ptr. flag.
TST.L (A2) ;Look @ left ptr. of current node.
BEQ.S SEARCH_NOFIND ;if zero. end routine w/o finding.
MOVEA.L (A2),A2 ;else load left ptr. into current ptr.
BRA.S CURRENT ;Look @ new node.
SEARCH_NOFIND CLR.W D1 ;Set found flag to false.
END_SEARCH RTS ;Return.
**** SUBROUTINE PUTNTREE ****
* This subroutine uses the search subroutine to see if there is already
* a node w/ the paramter value which is in d2. If there is not it uses
* the left/right flag, d3, to locate the postion where the new node
* should be inserted. When a new node is neccessary the provided sub.
* malloc is used to request 10 bytes of memory from the heap--4 bytes
* for the left-pointer address, 2 bytes for the integer, and 4 bytes
* for the right-pointer address. The node pointed to by a2, the last
* node looked @ by the search routine, has its left or right ptr.
* initialized based on the left/right flag to the ptr. of a0--returned
* by malloc as the block requested. The new node's pointers are both
* set to zero and the integer given the param value. Internal
* representation of integers is in ASCII.
******************************
**** RESOURCE ALLOCATION ****
* A0 -> pointer to new node, returned by malloc.
* A2 -> pointer to last node detected by search.
* D0 -> parameter for WriteChar, the character to be printed.
* D2 -> user input of the integer being looked for and inserted.
*****************************
**** SUBROUTINES ****
* CALLS: malloc
* SEARCH
* CALLED BY:MAIN
*********************
PUTNTREE MOVEA.L #$5000,A0 ;Load in top of tree.
CMPI.W #-1,4(A0) ;Look @ root node to see if there
BEQ.S FIRST_INSERT ;are any nodes, if not insert this as 1st.
JSR SEARCH ;else look for param. in tree.
TST.W D1 ;if param is found go to message.
BNE.S I_FOUNDIT ;by testing flag for true.
R_OR_L TST.W D3 ;test right/left flag for placement.
BEQ.S LEFT_INSERT ;if zero left insert.
MOVE.L #10,D0 ;else for right insert Load block.
JSR malloc ;and allocate memmory.
MOVE.L A0,6(A2) ;initiallize right pointer of node to
CLR.L (A0) ;new node, and clear pointers of new node.
CLR.L 6(A0) ;--clear right ptr.--
MOVE.W D2,4(A0) ;load integer.
BRA.S GOOD_IN ;end routine.
LEFT_INSERT MOVE.L #10,D0 ;load size for new block.
JSR malloc ;allocate memmory.
MOVE.L A0,(A2) ;initiallize left pointer of old node.
CLR.L (A0) ;clear pointers--left--
CLR.L 6(A0) ;--right--
MOVE.W D2,4(A0) ;load integer.
BRA.S GOOD_IN ;end routine.
FIRST_INSERT MOVE.W D2,4(A0) ;load integer--pointers previously init.
MOVE.L #10,D0 ;load size
JSR malloc ;allocate memmory and end routine.
GOOD_IN LEA GOOD_INMSG,A0 ;Load successful insert mess.
JSR WriteString ;Print out.
MOVE.B D2,D0 ;Load integer inserted.
JSR WriteChar ;Print out.
BRA.S END_PUTNTREE ;End routine.
I_FOUNDIT LEA INTEGERMSG,A0 ;Load message to print error
JSR WriteString ;write first word of message.
MOVE.B D2,D0 ;load to print out integer.
JSR WriteChar ;Write intger.
LEA I_FOUNDMSG,A0 ;Load tail of message.
JSR WriteString ;Write to screen.
END_PUTNTREE RTS ;End routine.
**** SUBROUTINE PRINTREE ****
* This subroutine prints the contents of the binary tree stored in the
* heap @ location $5000. The printing will use pre-order traversal.
*****************************
**** RESOURCE ALLOCATION ****
* A2 -> generic pointer to nodes.
* D0 -> parameter for WriteChar, the parameter to printed.
*****************************
**** SUBROUTINES ****
* CALLS: WriteEOL
* WriteChar
* CALLED BY: MAIN
*********************
PRINTREE JSR WriteEOL ;Print new line
MOVEA.L #$5000,A2 ;Load location of top of tree.
CMPI.W #-1,4(A2) ;Look @ top node if flag exit.
BEQ.S END_PRINTREE ;exit.
MOVE.L #-1,-(A7) ;Push end of print flag to stack.
P_LEFT MOVE.W 4(A2),D0 ;Load integer of node.
JSR WriteChar ;Print the integer out.
MOVE.W #' ',D0 ;Load in a space.
JSR WriteChar ;Print a space.
TST.L 6(A2) ;Test if right branch is valid.
BEQ.S P_NORIGHT ;if not valid(0) do not push address
MOVE.L 6(A2),-(A7) ;else push address of right to stack.
P_NORIGHT TST.L (A2) ;look @left
BEQ.S NO_LEFT ;if left ptr. is not valid go no_left.
MOVEA.L (A2),A2 ;else load in left ptr. to new node.
BRA.S P_LEFT ;stay in left print loop.
NO_LEFT MOVE.L (A7)+,A2 ;load in last valid right node.
CMPA.L #-1,A2 ;compare address to end of print flag.
BEQ.S END_PRINTREE ;if eopf then end pritree.
BRA.S P_LEFT ;else use right node as current node.
END_PRINTREE RTS ;return from sub.
*******************
**** VARIABLES ****
*******************
CNOP 0,2
INTEGERMSG DC.B 13,10,'Integer ',0
CNOP 0,2
S_FOUNDMSG DC.B ' was found in search tree.',13,10,0
CNOP 0,2
S_NOFINDMSG DC.B ' was not found.',13,10,0
CNOP 0,2
COMMANDMSG DC.B 'Please enter command: ',0
CNOP 0,2
I_FOUNDMSG DC.B ' already exists in tree.',13,10,0
CNOP 0,2
MANY_DMSG DC.B 13,10,'Integer too large, 0-9 only!!!',13,10
DC.B 'or Command does not accept an integer.',13,10,0
CNOP 0,2
BAD_MSG DC.B 13,10,'INVALID COMMAND!!!-Please try again.',0
CNOP 0,2
GOOD_INMSG DC.B 13,10,'Successfull insertion of ',0
CNOP 0,2
NO_DMSG DC.B 13,10,'No Digits were found.',13,10,0
CNOP 0,2
FOUND_DIGIT DC.W 0
COMMAND DC.B 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
END